package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostBannerDao;
import ysq.xyg.host.model.po.HostBanner;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostBannerVo;
import ysq.xyg.host.service.HostBannerService;

import java.util.Date;
import java.util.List;

/**
 * 房产-轮播图service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:25:49
 */
@Service("hostBannerService")
public class HostBannerServiceImpl extends BaseServiceImpl<HostBanner> implements HostBannerService {

    @Autowired
    private HostBannerDao hostBannerDao;

    @Autowired
    public void setBaseDao(HostBannerDao hostBannerDao) {
        this.baseDao = hostBannerDao;
    }

    /**
     * 分页查询
     *
     * @param hostBannerVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:49
     */
    @Override
    public ReturnEntity page(HostBannerVo hostBannerVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" order_num asc, update_time desc");
        List<HostBanner> list = acceptList(hostBannerVo);
        PageInfo<HostBanner> page = new PageInfo<HostBanner>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostBannerVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:49
     */
    @Override
    @CacheEvict(value={"cacheHostBannerByCityIdList"}, allEntries=true)
    public ReturnEntity addOrUpdate(HostBannerVo hostBannerVo, SysUser currentUser) {
        boolean isUpate = true;
        HostBanner hostBanner;
        if (StrUtil.isEmpty(hostBannerVo.getId())) {
            hostBanner = new HostBanner(currentUser);
            isUpate = false;
        } else {
            hostBanner = selectByPrimaryKey(hostBannerVo.getId());
            if (ObjectUtil.isNull(hostBanner)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostBanner.setUpdateTime(new Date());
            hostBanner.setUpdateUser(currentUser.getId());
        }
        hostBanner.setCityId(hostBannerVo.getCityId());
        hostBanner.setScopeType(hostBannerVo.getScopeType());
        hostBanner.setType(hostBannerVo.getType());
        hostBanner.setUrl(hostBannerVo.getUrl());
        hostBanner.setJumpUrl(hostBannerVo.getJumpUrl());
        hostBanner.setJumpType(hostBannerVo.getJumpType());
        hostBanner.setShowType(hostBannerVo.getShowType());
        hostBanner.setOrderNum(hostBannerVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostBanner);
        } else {
            num = insert(hostBanner);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:49
     */
    @Override
    @CacheEvict(value={"cacheHostBannerByCityIdList"}, allEntries=true)
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostBanner hostBanner = selectByPrimaryKey(id);
        if (hostBanner != null) {
            hostBanner.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostBanner.setUpdateTime(new Date());
            hostBanner.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostBanner);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostBannerVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:25:49
     */
    @Override
    public List<HostBanner> acceptList(HostBannerVo hostBannerVo){
        Example example = new Example(HostBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostBannerVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostBannerVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostBannerVo.getCityId())) {
            criteria.andEqualTo("cityId", hostBannerVo.getCityId());
        }
        if (StringUtil.isNotEmpty(hostBannerVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostBannerVo.getAccountId());
        }
        example.setOrderByClause(" order_num asc, update_time desc");
        return hostBannerDao.selectByExample(example);
    }
}
